package io.renren.modules.archive.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.renren.common.utils.StringUtils;
import io.renren.modules.archive.entity.ArchiveCollectEntity;
import io.renren.modules.archive.entity.MeetingDetailEntity;
import io.renren.modules.archive.entity.MeetingtypeEntity;
import io.renren.modules.archive.service.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 会议详情表
 *
 * @author lizeliang
 * @email 2749877017@qq.com
 * @date 2023-08-21 17:43:14
 */
@RestController
@RequestMapping("archive/meetingdetail")
public class MeetingDetailController {
    @Autowired
    private MeetingDetailService meetingDetailService;
    @Autowired
    private DictionaryService dictionaryService;
    @Autowired
    private BgqxService bgqxService;
    @Autowired
    private MeetingtypeService meetingtypeService;
    @Autowired
    private ArchiveCollectService archiveCollectService;
    /**
     * 列表
     */
    @RequestMapping("/list")
//    @RequiresPermissions("archive:meetingdetail:list")
    public R list(@RequestParam Map<String, Object> params){
        String level=(String)params.get("level");
        String menuId=(String)params.get("menuId");
        String nd=(String)params.get("nd");
        String jg=(String)params.get("jg");
        String onesxlx="";
        String twosxlx="";
        String threesxlx="";
        String foursxlx="";
        List<ArchiveCollectEntity> menuList=new ArrayList<>();
        if(StringUtils.isNotBlank(level)){
            menuList=archiveCollectService.queryLocalMenu(params);
        }else{
            menuList=archiveCollectService.queryLocalMenuHaveLevelNull(params);
        }

        if(menuList.size()>0){
            if(menuList.size()==1){
                onesxlx= menuList.get(0).getValue();
            }else if(menuList.size()==2){
                onesxlx= menuList.get(menuList.size()-1).getValue();
                twosxlx= menuList.get(menuList.size()-2).getValue();
            }else if(menuList.size()==3){
                onesxlx= menuList.get(menuList.size()-1).getValue();
                twosxlx= menuList.get(menuList.size()-2).getValue();
                if(StringUtils.isNotBlank(level)){
                    threesxlx= menuList.get(0).getValue();
                }else{
                    threesxlx= menuList.get(0).getValue().substring(0,1);
                }

            }else{
                onesxlx= menuList.get(menuList.size()-1).getValue();
                twosxlx= menuList.get(menuList.size()-2).getValue();
                threesxlx= menuList.get(menuList.size()-3).getValue();
                foursxlx= menuList.get(0).getValue();
            }
        }
        params.put("onesxlx",onesxlx);
        params.put("twosxlx",twosxlx);
        params.put("threesxlx",threesxlx);
        params.put("foursxlx",foursxlx);
        PageUtils page = meetingDetailService.queryPage(params);
        List<MeetingDetailEntity> meetingDetailList= (List<MeetingDetailEntity>) page.getList();

        for (MeetingDetailEntity meetingDetailEntity:meetingDetailList
        ) {
            String one =meetingDetailEntity.getOnesxlx();
            String two =meetingDetailEntity.getTwosxlx();
            String three =meetingDetailEntity.getThreesxlx();
            String four =meetingDetailEntity.getFoursxlx();


            if(StringUtils.isNotBlank(meetingDetailEntity.getJg())){
                meetingDetailEntity.setJg(dictionaryService.queryBydictionaryValue(meetingDetailEntity.getJg(),"nsjgmc"));
            }
            if(StringUtils.isNotBlank(meetingDetailEntity.getGeocode())){
                meetingDetailEntity.setGeocode(dictionaryService.queryBydictionaryValue(meetingDetailEntity.getGeocode(),"geocode"));
            }
            if(StringUtils.isNotBlank(meetingDetailEntity.getBgqx())){
                meetingDetailEntity.setBgqx(dictionaryService.queryBydictionaryValue(bgqxService.getById(meetingDetailEntity.getBgqx()).getBgqx(),"bgqx"));
            }





           String foursxlxName= meetingtypeService.list(new QueryWrapper<MeetingtypeEntity>()
                    .eq(StringUtils.isNotBlank(one), "onesxlx", one)
                    .eq(StringUtils.isNotBlank(two), "twosxlx", two)
                    .eq(StringUtils.isNotBlank(three), "threesxlx",three)
                    .eq(StringUtils.isNotBlank(four), "foursxlx",four)
                   .eq(StringUtils.isNotBlank(nd), "meetingtype_nd", nd)
                   .eq(StringUtils.isNotBlank(jg), "meetingtype_jg", jg)
            ).get(0).getMeetingtypeName();

            meetingDetailEntity.setFoursxlx(foursxlxName);
        }

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{meetingDetailId}")
//    @RequiresPermissions("archive:meetingdetail:info")
    public R info(@PathVariable("meetingDetailId") Long meetingDetailId){
		MeetingDetailEntity meetingDetail = meetingDetailService.getById(meetingDetailId);
        return R.ok().put("meetingDetail", meetingDetail);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
//    @RequiresPermissions("archive:meetingdetail:save")
    public R save(@RequestBody MeetingDetailEntity meetingDetail){
        meetingDetail.setSxlxcode(meetingDetail.getOnesxlx()+"-"
        +meetingDetail.getTwosxlx()+"-"+meetingDetail.getThreesxlx()
         +"-"+meetingDetail.getFoursxlx()
       );
		meetingDetailService.save(meetingDetail);
        MeetingDetailEntity m=meetingDetailService.getById(meetingDetail.getMeetingDetailId());
        if(StringUtils.isNotBlank(m.getGeocode())&&StringUtils.isNotBlank(m.getJg())
                &&StringUtils.isNotBlank(m.getOnesxlx())
                &&StringUtils.isNotBlank(m.getTwosxlx())
                &&StringUtils.isNotBlank(m.getThreesxlx())
                &&StringUtils.isNotBlank(m.getFoursxlx())
                &&StringUtils.isNotBlank(m.getOrdernum())
                &&StringUtils.isNotBlank(m.getNd())
                &&StringUtils.isNotBlank(m.getBgqx())
        ){
            String attachId=m.getNd()+"-"+m.getJg()+"-"+m.getOnesxlx()+"-"+m.getTwosxlx()
                    +"-"+m.getThreesxlx()+"-"+m.getFoursxlx()
                    +"-"+m.getOrdernum();
            m.setAttachId(attachId);
        }
        meetingDetailService.updateById(m);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
//    @RequiresPermissions("archive:meetingdetail:update")
    public R update(@RequestBody MeetingDetailEntity meetingDetail){
        meetingDetail.setSxlxcode(meetingDetail.getOnesxlx()+"-"
                +meetingDetail.getTwosxlx()+"-"+meetingDetail.getThreesxlx()
                +"-"+meetingDetail.getFoursxlx()
        );
        if(StringUtils.isNotBlank(meetingDetail.getGeocode())&&StringUtils.isNotBlank(meetingDetail.getJg())
                &&StringUtils.isNotBlank(meetingDetail.getOnesxlx())
                &&StringUtils.isNotBlank(meetingDetail.getTwosxlx())
                &&StringUtils.isNotBlank(meetingDetail.getThreesxlx())
                &&StringUtils.isNotBlank(meetingDetail.getFoursxlx())
                &&StringUtils.isNotBlank(meetingDetail.getOrdernum())
                &&StringUtils.isNotBlank(meetingDetail.getNd())
                &&StringUtils.isNotBlank(meetingDetail.getBgqx())
        ){
            String attachId=meetingDetail.getNd()+"-"+meetingDetail.getJg()+"-"+meetingDetail.getOnesxlx()+"-"+meetingDetail.getTwosxlx()
                    +"-"+meetingDetail.getThreesxlx()+"-"+meetingDetail.getFoursxlx()
                    +"-"+meetingDetail.getOrdernum();
            meetingDetail.setAttachId(attachId);
        }
		meetingDetailService.updateById(meetingDetail);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
//    @RequiresPermissions("archive:meetingdetail:delete")
    public R delete(@RequestBody Long[] meetingDetailIds){
		meetingDetailService.removeByIds(Arrays.asList(meetingDetailIds));

        return R.ok();
    }


    /**
     * 档案查询
     */
    @RequestMapping("/query")
//    @RequiresPermissions("archive:meetingdetail:query")
    public R query(@RequestParam Map<String, Object> params){
        PageUtils page = meetingDetailService.conditionQueryPage(params);

        if(page.getList().size()==0){
            Page pag=(Page)params.get("page");

            if(pag.getCurrent()>1){
                long i=pag.getCurrent()-1;
                params.put("page",Long.toString(i));
                page = meetingDetailService.queryPage(params);
            }
        }
        List<MeetingDetailEntity> meetingDetailList= (List<MeetingDetailEntity>) page.getList();
        for (MeetingDetailEntity meetingDetailEntity:meetingDetailList
        ) {

            LambdaQueryWrapper<MeetingtypeEntity> lambdaQueryWrapper= Wrappers.lambdaQuery();
            lambdaQueryWrapper .eq(StringUtils.isNotBlank(meetingDetailEntity.getOnesxlx()), MeetingtypeEntity::getOnesxlx, meetingDetailEntity.getOnesxlx())
                    .eq(StringUtils.isNotBlank(meetingDetailEntity.getTwosxlx()), MeetingtypeEntity::getTwosxlx, meetingDetailEntity.getTwosxlx())
                    .eq(StringUtils.isNotBlank(meetingDetailEntity.getThreesxlx()), MeetingtypeEntity::getThreesxlx, meetingDetailEntity.getThreesxlx())
                    .eq(StringUtils.isNotBlank(meetingDetailEntity.getFoursxlx()), MeetingtypeEntity::getFoursxlx, meetingDetailEntity.getFoursxlx())
                    .eq(StringUtils.isNotBlank(meetingDetailEntity.getJg()),  MeetingtypeEntity::getMeetingtypeJg, meetingDetailEntity.getJg())
                    .eq(StringUtils.isNotBlank(meetingDetailEntity.getNd()),  MeetingtypeEntity::getMeetingtypeNd, meetingDetailEntity.getNd())
            ;
            String foursxlxName= meetingtypeService.list(lambdaQueryWrapper).get(0).getMeetingtypeName();

            meetingDetailEntity.setFoursxlx(foursxlxName);



            if(StringUtils.isNotBlank(meetingDetailEntity.getJg())){
                meetingDetailEntity.setJg(dictionaryService.queryBydictionaryValue(meetingDetailEntity.getJg(),"nsjgmc"));
            }
            if(StringUtils.isNotBlank(meetingDetailEntity.getGeocode())){
                meetingDetailEntity.setGeocode(dictionaryService.queryBydictionaryValue(meetingDetailEntity.getGeocode(),"geocode"));
            }
            if(StringUtils.isNotBlank(meetingDetailEntity.getBgqx())){
                meetingDetailEntity.setBgqx(dictionaryService.queryBydictionaryValue(bgqxService.getById(meetingDetailEntity.getBgqx()).getBgqx(),"bgqx"));
            }




        }

        return R.ok().put("page", page);
    }


}
